package teacher.dao;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import teacher.entity.Teacher;

/*
 * 数据访问层，对数据的操作
 */
public class TeacherDao {

	public void addTeacher(Teacher teacher, String path) throws IOException {

		List<Teacher> teachers = selectAllTeacher(path);
		Integer id = Integer.valueOf(teachers.get(teachers.size() - 1).getId());
		try (PrintWriter outputPrintWriter = new PrintWriter(new FileOutputStream(path, true))) {
			String teaString = ("\r\n" + (id + 1) + " " + teacher.getName() + " " + teacher.getCollege() + " "
					+ teacher.getProfession() + " " + teacher.getGrade() + " " + teacher.getSalary() + " "
					+ teacher.getAge());
			System.out.print(teaString);
			outputPrintWriter.println(teaString);
		} catch (Exception e) {

		}

	}

	public void deleteTeacher(Integer id, String path) throws IOException {
		
		List<Teacher> teachers = selectAllTeacher(path);
		for (int i = 0; i < teachers.size(); i++) {
			if (id.equals(teachers.get(i).getId())) {
				teachers.remove(i);
			}
		}
		File file = new File(path);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file);
			fileWriter.write("");
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		int i = 0;
		String teaString;
		for (Teacher teacher1 : teachers) {
			try (PrintWriter outputPrintWriter = new PrintWriter(new FileOutputStream(path, true))) {
				if (i == 0) {
					teaString = (teacher1.getId() + " " + teacher1.getName() + " " + teacher1.getCollege() + " "
							+ teacher1.getProfession() + " " + teacher1.getGrade() + " " + teacher1.getSalary() + " "
							+ teacher1.getAge());
					outputPrintWriter.println(teaString);
				} else {
					teaString = ("\r\n" + teacher1.getId() + " " + teacher1.getName() + " " + teacher1.getCollege()
							+ " " + teacher1.getProfession() + " " + teacher1.getGrade() + " " + teacher1.getSalary()
							+ " " + teacher1.getAge());
					System.out.print(teaString);
					outputPrintWriter.println(teaString);
				}
			} catch (Exception e) {

			}
		}
	}

	public void updateTeacher(Teacher teacher, String path) throws IOException {

		List<Teacher> teachers = selectAllTeacher(path);
		for (Teacher teacher1 : teachers) {
			if (teacher.getId().equals(teacher1.getId())) {
				teacher1.setName(teacher.getName());
				teacher1.setCollege(teacher.getCollege());
				teacher1.setProfession(teacher.getProfession());
				teacher1.setGrade(teacher.getGrade());
				teacher1.setAge(teacher.getAge());
			}
		}

		File file = new File(path);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file);
			fileWriter.write("");
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		int i = 0;
		String teaString;
		for (Teacher teacher1 : teachers) {
			try (PrintWriter outputPrintWriter = new PrintWriter(new FileOutputStream(path, true))) {
				if (i == 0) {
					teaString = (teacher1.getId() + " " + teacher1.getName() + " " + teacher1.getCollege() + " "
							+ teacher1.getProfession() + " " + teacher1.getGrade() + " " + teacher1.getSalary() + " "
							+ teacher1.getAge());
					outputPrintWriter.println(teaString);
				} else {
					teaString = ("\r\n" + teacher1.getId() + " " + teacher1.getName() + " " + teacher1.getCollege()
							+ " " + teacher1.getProfession() + " " + teacher1.getGrade() + " " + teacher1.getSalary()
							+ " " + teacher1.getAge());
					System.out.print(teaString);
					outputPrintWriter.println(teaString);
				}
			} catch (Exception e) {

			}
		}

	}

	public Teacher findTeacherById(Integer id, String path) throws IOException {

		List<Teacher> teachers = selectAllTeacher(path);
		for (Teacher teacher : teachers) {
			if (id.equals(teacher.getId())) {
				return teacher;
			}
		}
		return null;
	}

	@SuppressWarnings("resource")
	public List<Teacher> selectAllTeacher(String path) throws IOException {

		List<Teacher> teachers = new ArrayList<Teacher>();
		File file = new File(path);
		InputStreamReader read = null;
		BufferedReader reader = null;

		read = new InputStreamReader(new FileInputStream(file), "utf-8");
		reader = new BufferedReader(read);
		String line;
		while ((line = reader.readLine()) != null) {
			Teacher teacher = new Teacher();
			String[] teaString = line.split("\\s+");
			teacher.setId(Integer.parseInt(teaString[0]));
			teacher.setName(teaString[1]);
			teacher.setCollege(teaString[2]);
			teacher.setProfession(teaString[3]);
			teacher.setGrade(teaString[4]);
			teacher.setSalary(Double.valueOf(teaString[5]));
			teacher.setAge(Integer.valueOf(teaString[6]));

			teachers.add(teacher);
		}

		return teachers;
	}

	// 按照id排序
	public void sortById(List<Teacher> teachers) {
		Collections.sort(teachers, new Comparator<Teacher>() {

			@Override
			public int compare(Teacher o1, Teacher o2) {
				return o1.getId().compareTo(o2.getId());
			}

		});
	}

	// 按照年龄排序
	public void sortByAge(List<Teacher> teachers) {
		Collections.sort(teachers, new Comparator<Teacher>() {

			@Override
			public int compare(Teacher o1, Teacher o2) {
				return o1.getAge().compareTo(o2.getAge());
			}

		});
	}

	// 获取当前页的老师
	public List<Teacher> currentPageTeachers(List<Teacher> teachers, Integer currentPage) {
		List<Teacher> newTeachers = new ArrayList<Teacher>();
		Integer teacherNumbers = teachers.size();
		// Integer totalPages = (teacherNumbers % 10 == 0) ? (teacherNumbers / 10) :
		// (teacherNumbers / 10 + 1);

		// 如果当前学生小于10，直接将其余学生加入newTeachers，否则新增10个到newTeachers
		if (currentPage * 10 > teacherNumbers) {
			for (int i = (currentPage - 1) * 10; i < teacherNumbers; i++) {
				newTeachers.add(teachers.get(i));
			}
		} else {
			for (int i = (currentPage - 1) * 10; i < ((currentPage - 1) * 10 + 10); i++) {
				newTeachers.add(teachers.get(i));
			}
		}

		return newTeachers;
	}
}
